package com.itheima.common;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;

/**
 * @author :wujunjie
 * createTime :2022-12-23 15:24
 * description :
 */
public class TimeUtil {


    public static String handleTime(LocalDateTime createTime){
        LocalDateTime now = LocalDateTime.now();

        LocalDate nowDate = now.toLocalDate();
        LocalDate CreateDate = createTime.toLocalDate();
        Period between = Period.between(CreateDate, nowDate);
        int days = between.getDays();
        int months = between.getMonths();
        int years = between.getYears();
        if (years>0){
            return years+"年前";
        }
        if (months>0){
            return months+"个月前";
        }
        if (days>0){
            return days+"天前";
        }

        Duration duration = Duration.between(createTime, now);
        long seconds = duration.toSeconds();
        long minutes = duration.toMinutes();
        long hours = duration.toHours();

        if (hours>0){
            return hours+"小时前";
        }
        if (minutes>0){
            return minutes + "分钟前";
        }
        if (seconds>0){
            return minutes+"秒前";
        }
        return "时间异常";
    }
}
